package com.googlecode.ozonbooksfinder.cairngormfinder.command
{
	import com.adobe.cairngorm.commands.ICommand;
	import com.adobe.cairngorm.control.CairngormEvent;
	import com.googlecode.ozonbooksfinder.cairngormfinder.business.BookDetailsDelegate;
	import com.googlecode.ozonbooksfinder.cairngormfinder.event.GetBookInfoEvent;
	import com.googlecode.ozonbooksfinder.cairngormfinder.model.CairngormFinderModelLocator;
	import com.googlecode.ozonbooksfinder.cairngormfinder.utils.BuildStronglyTypedBookVO;
	import com.googlecode.ozonbooksfinder.domain.Book;
	
	import mx.rpc.IResponder;
	
	public class GetBookInfoCommand implements ICommand, IResponder
	{
		private var model:CairngormFinderModelLocator = CairngormFinderModelLocator.getInstance();
		
		private var bookToken:Book;
		
		public function execute(event:CairngormEvent):void
	   	{
	   		var delegate:BookDetailsDelegate = new BookDetailsDelegate(this);
	   		bookToken = GetBookInfoEvent(event).book;
	   		model.currentBook = bookToken;
	   		delegate.getItemInfo(bookToken.id);
	   	}
	   	public function result(event:Object):void
		{
			var book:Book = BuildStronglyTypedBookVO.build(event.result.Tables.ItemInfo.Rows[0]);
			bookToken.addExtendedDetails(book);
		}
	   	public function fault(event:Object):void
		{
			trace( "GetBookInfo Error:\n" + event.fault.faultDetail );
		}
	}
}